#include "board.h"

//(*InternalHeaders(board)
#include <wx/bitmap.h>
#include <wx/font.h>
#include <wx/fontenum.h>
#include <wx/fontmap.h>
#include <wx/image.h>
#include <wx/intl.h>
#include <wx/settings.h>
//*)

#include "connection.h"
#include "winner.h"

//(*IdInit(board)
const long board::ID_BUTTON1 = wxNewId();
const long board::ID_BUTTON2 = wxNewId();
const long board::ID_BUTTON3 = wxNewId();
const long board::ID_BUTTON4 = wxNewId();
const long board::ID_BUTTON5 = wxNewId();
const long board::ID_BUTTON6 = wxNewId();
const long board::ID_BUTTON7 = wxNewId();
const long board::ID_BUTTON8 = wxNewId();
const long board::ID_BUTTON9 = wxNewId();
//*)

BEGIN_EVENT_TABLE(board,wxFrame)
	//(*EventTable(board)
	//*)
END_EVENT_TABLE()

board::board(wxWindow* parent, int roomID, Connection* serv, wxWindowID id)
{
	roomid = roomID;
	server = serv;
	//(*Initialize(board)
	Create(parent,id,_("Game"),wxDefaultPosition,wxDefaultSize,wxDEFAULT_FRAME_STYLE,_T("wxFrame"));
	SetClientSize(wxSize(296,266));
	GridSizer1 = new wxGridSizer(3,3,0,0);
	Button1 = new wxButton(this,ID_BUTTON1,wxEmptyString,wxDefaultPosition,wxSize(85,75),0,wxDefaultValidator,_T("ID_BUTTON1"));
	wxFont Button1Font(36,wxSWISS,wxFONTSTYLE_NORMAL,wxNORMAL,false,_("Sans"),wxFONTENCODING_DEFAULT);
	Button1->SetFont(Button1Font);
	GridSizer1->Add(Button1,1,wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL,5);
	Button2 = new wxButton(this,ID_BUTTON2,wxEmptyString,wxDefaultPosition,wxSize(85,75),0,wxDefaultValidator,_T("ID_BUTTON2"));
	wxFont Button2Font(36,wxSWISS,wxFONTSTYLE_NORMAL,wxNORMAL,false,_("Sans"),wxFONTENCODING_DEFAULT);
	Button2->SetFont(Button2Font);
	GridSizer1->Add(Button2,1,wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL,5);
	Button3 = new wxButton(this,ID_BUTTON3,wxEmptyString,wxDefaultPosition,wxSize(85,75),0,wxDefaultValidator,_T("ID_BUTTON3"));
	wxFont Button3Font(36,wxSWISS,wxFONTSTYLE_NORMAL,wxNORMAL,false,_("Sans"),wxFONTENCODING_DEFAULT);
	Button3->SetFont(Button3Font);
	GridSizer1->Add(Button3,1,wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL,5);
	Button4 = new wxButton(this,ID_BUTTON4,wxEmptyString,wxDefaultPosition,wxSize(85,75),0,wxDefaultValidator,_T("ID_BUTTON4"));
	wxFont Button4Font(36,wxSWISS,wxFONTSTYLE_NORMAL,wxNORMAL,false,_("Sans"),wxFONTENCODING_DEFAULT);
	Button4->SetFont(Button4Font);
	GridSizer1->Add(Button4,1,wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL,5);
	Button5 = new wxButton(this,ID_BUTTON5,wxEmptyString,wxDefaultPosition,wxSize(85,75),0,wxDefaultValidator,_T("ID_BUTTON5"));
	wxFont Button5Font(36,wxSWISS,wxFONTSTYLE_NORMAL,wxNORMAL,false,_("Sans"),wxFONTENCODING_DEFAULT);
	Button5->SetFont(Button5Font);
	GridSizer1->Add(Button5,1,wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL,5);
	Button6 = new wxButton(this,ID_BUTTON6,wxEmptyString,wxDefaultPosition,wxSize(85,75),0,wxDefaultValidator,_T("ID_BUTTON6"));
	wxFont Button6Font(36,wxSWISS,wxFONTSTYLE_NORMAL,wxNORMAL,false,_("Sans"),wxFONTENCODING_DEFAULT);
	Button6->SetFont(Button6Font);
	GridSizer1->Add(Button6,1,wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL,5);
	Button7 = new wxButton(this,ID_BUTTON7,wxEmptyString,wxDefaultPosition,wxSize(85,76),0,wxDefaultValidator,_T("ID_BUTTON7"));
	wxFont Button7Font(36,wxSWISS,wxFONTSTYLE_NORMAL,wxNORMAL,false,_("Sans"),wxFONTENCODING_DEFAULT);
	Button7->SetFont(Button7Font);
	GridSizer1->Add(Button7,1,wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL,5);
	Button8 = new wxButton(this,ID_BUTTON8,wxEmptyString,wxDefaultPosition,wxSize(85,75),0,wxDefaultValidator,_T("ID_BUTTON8"));
	wxFont Button8Font(36,wxSWISS,wxFONTSTYLE_NORMAL,wxNORMAL,false,_("Sans"),wxFONTENCODING_DEFAULT);
	Button8->SetFont(Button8Font);
	GridSizer1->Add(Button8,1,wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL,5);
	Button9 = new wxButton(this,ID_BUTTON9,wxEmptyString,wxDefaultPosition,wxSize(85,75),0,wxDefaultValidator,_T("ID_BUTTON9"));
	wxFont Button9Font(36,wxSWISS,wxFONTSTYLE_NORMAL,wxNORMAL,false,_("Sans"),wxFONTENCODING_DEFAULT);
	Button9->SetFont(Button9Font);
	GridSizer1->Add(Button9,1,wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL,5);
	SetSizer(GridSizer1);
	GridSizer1->SetSizeHints(this);
	Connect(wxID_ANY,wxEVT_CLOSE_WINDOW,(wxObjectEventFunction)&board::OnClose);
	Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&board::OnButtonULClick);
	Connect(ID_BUTTON2,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&board::OnButtonUClick);
	Connect(ID_BUTTON3,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&board::OnButtonURClick);
	Connect(ID_BUTTON4,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&board::OnButtonLClick);
	Connect(ID_BUTTON5,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&board::OnButtonCClick);
	Connect(ID_BUTTON6,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&board::OnButtonRClick);
	Connect(ID_BUTTON7,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&board::OnButtonLLClick);
	Connect(ID_BUTTON8,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&board::L);
	Connect(ID_BUTTON9,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&board::OnButtonLRClick);
	//*)

	scoreboard = new Info(NULL);
	scoreboard->Show();
}

board::~board()
{
	server->leave(roomid);
	server->removeBoard(this);
    delete scoreboard;
	//(*Destroy(board)
	//*)
}


void board::OnClose(wxCloseEvent& event)
{
	delete this;
}

void board::OnButtonULClick(wxCommandEvent& event)
{
    server->sendMove(UPPERLEFT, roomid);
}

void board::OnButtonUClick(wxCommandEvent& event)
{
    server->sendMove(UPPER, roomid);
}

void board::OnButtonURClick(wxCommandEvent& event)
{
    server->sendMove(UPPERRIGHT, roomid);
}

void board::OnButtonLClick(wxCommandEvent& event)
{
    server->sendMove(LEFT, roomid);
}

void board::OnButtonCClick(wxCommandEvent& event)
{
    server->sendMove(CENTER, roomid);
}

void board::OnButtonRClick(wxCommandEvent& event)
{
    server->sendMove(RIGHT, roomid);
}

void board::OnButtonLLClick(wxCommandEvent& event)
{
    server->sendMove(LOWERLEFT, roomid);
}

void board::L(wxCommandEvent& event)
{
    server->sendMove(LOWER, roomid);
}

void board::OnButtonLRClick(wxCommandEvent& event)
{
    server->sendMove(LOWERRIGHT, roomid);
}

int board::getRoomID()
{
    return roomid;
}

void board::move(CODE cmd)
{
    wxString* symbol;
    if(cmd & X) {
         symbol = new wxString("X", wxConvLocal);
         cmd = cmd &~ X;
    }else if(cmd & O) {
        symbol = new wxString("O", wxConvLocal);
        cmd = cmd &~ O;
    }else return;

    switch(cmd)
    {
        case UPPERLEFT:
            Button1->SetLabel(*symbol);
            Button1->Enable(false);
            break;
        case UPPER:
            Button2->SetLabel(*symbol);
            Button2->Enable(false);
            break;
        case UPPERRIGHT:
            Button3->SetLabel(*symbol);
            Button3->Enable(false);
            break;
        case LEFT:
            Button4->SetLabel(*symbol);
            Button4->Enable(false);
            break;
        case CENTER:
            Button5->SetLabel(*symbol);
            Button5->Enable(false);
            break;
        case RIGHT:
            Button6->SetLabel(*symbol);
            Button6->Enable(false);
            break;
        case LOWERLEFT:
            Button7->SetLabel(*symbol);
            Button7->Enable(false);
            break;
        case LOWER:
            Button8->SetLabel(*symbol);
            Button8->Enable(false);
            break;
        case LOWERRIGHT:
            Button9->SetLabel(*symbol);
            Button9->Enable(false);
            break;
    }
	delete symbol;
}

void board::gameWon(int xoro)
{
    winner* dialog = new winner(NULL);
    dialog->Show();

    //server->join(room);
}
